package com.cu.employmentmanagementsystem.util;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author 25853
 */
public class CaptchaUtils
{
    public static ShearCaptcha generateCaptcha()
    {

        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
        // 自定义验证码内容为四则运算方式
//        captcha.setGenerator(new MathGenerator());
        // 重新生成code
        captcha.createCode();
//        RandomGenerator randomGenerator = new RandomGenerator("智方典当行", 5);
//        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
//        lineCaptcha.setGenerator(randomGenerator);
//        lineCaptcha.createCode();
        return captcha;
    }

    public static String calculateExpression(String expression)
    {
        // 检查输入字符串是否符合要求
        expression = expression.trim();

        if (!expression.matches("^\\d+[\\-+*/]\\d+="))
        {
            System.out.println("无效的表达式");
            return "-1";
        }

        // 提取操作数和操作符
        Pattern numberPattern = Pattern.compile("\\d+");
        Matcher numberMatcher = numberPattern.matcher(expression);
        int[] numbers = new int[2];
        int index = 0;

        while (numberMatcher.find())
        {
            numbers[index] = Integer.parseInt(numberMatcher.group());
            index++;
        }

        char operator = expression.charAt(expression.indexOf(numbers[1] + "") - 1);

        // 执行四则运算
        int result;

        switch (operator)
        {
            case '+':
                result = numbers[0] + numbers[1];
                break;
            case '-':
                result = numbers[0] - numbers[1];
                break;
            case '*':
                result = numbers[0] * numbers[1];
                break;
            case '/':
                result = numbers[0] / numbers[1];
                break;
            default:
                return "-1";
        }

        return String.valueOf(result);
    }
}
